package com.xiaoxx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxx.coin.enums.OrderStatusEnum;
import com.xiaoxx.mapper.BinanceOderMapper;
import com.xiaoxx.model.entity.coin.BinanceOrder;
import com.xiaoxx.service.BinanceOderService;
import com.xiaoxx.utils.date.LocalDateTimeUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author HX
 * @since 2024-12-09
 */
@Service
public class BinanceOderServiceImpl extends ServiceImpl<BinanceOderMapper, BinanceOrder> implements BinanceOderService {

    @Override
    public BigDecimal getTodayProfitAndLoss(String userId) {
        LocalDateTime dayStart = LocalDateTimeUtil.getDayStart(LocalDateTime.now());
        LocalDateTime dayEnd = LocalDateTimeUtil.getDayEnd(LocalDateTime.now());
        return lambdaQuery()
                .eq(BinanceOrder::getUserId, userId)
                .eq(BinanceOrder::getStatus, OrderStatusEnum.FILLED.getCode())
                .between(BinanceOrder::getCreateTime, dayStart, dayEnd)
                .list()
                .stream().map(BinanceOrder::getProfit)
                .filter(Objects::nonNull).map(profit -> new BigDecimal(profit)) // 转换字符串为 BigDecimal
                .reduce(BigDecimal.ZERO, BigDecimal::add) // 累加利润值
                .setScale(2, BigDecimal.ROUND_HALF_UP);
    }
}
